package com.gxc.webcore.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 可序列化对象与byte数组、String的相互转换
 * 
 * @author gxc
 * 
 */
public class SerializeUtil {

	public byte[] object2byteArray(Object object) {
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream out = new ObjectOutputStream(bos);
			out.writeObject(object);
			return bos.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Object byteArray2object(byte[] byteArray) {
		try {
			ByteArrayInputStream ios = new ByteArrayInputStream(byteArray);
			ObjectInputStream in = new ObjectInputStream(ios);
			return in.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String object2hexString(Object object) {
		return StringUtil.byteArray2hexString(this.object2byteArray(object));
	}

	public Object hexString2object(String hexString) {
		return this.byteArray2object(StringUtil.hexString2byteArray(hexString));
	}

}
